﻿using M_ApiResult;
using M_Quality.API.Application.Command.QualitygroupCommand;
using M_Quality.API.Application.Command.QualityPlanCommand;
using M_Quality.Domain;
using M_Quality.Domain.Dto;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace M_Quality.API.Controllers
{
    /// <summary>
    /// 质检方案控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class QualityPianController : ControllerBase
    {

        protected readonly IMediator mediator;

        public QualityPianController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 根据质检组ID显示质检项目
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<QuDto>> GetPList([FromQuery]QualityprogramShow shows)
        {
            ApiResult<List<QuDto>> result = new ApiResult<List<QuDto>>();
            try
            {
                result.Data = await mediator.Send(shows);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result.Data;
        }
     
        /// <summary>
        /// 显示质检方案
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<QualityPlan>>> GetQualityPlanList([FromQuery]QualityPianListCommand command)
        {
            ApiResult<PageDto<QualityPlan>> result = new ApiResult<PageDto<QualityPlan>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code= -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
  
        /// <summary>
        /// 修改质检信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateQualityPlan(QualityPianUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改质检方案信息失败";
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改质检方案状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateQualityPlanSarte([FromQuery]QualityPianUpdateSarteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "修改质检方案状态失败";
                throw;
            }
            return result;
        }
    }
}
